//
//  JYShopsInfoController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/12.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopsInfoController.h"
#import "JYSettingPwdView.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import "HDPickerView.h"
#import "JYTimePickerView.h"
@interface JYShopsInfoController ()

//商家名称
@property(nonatomic)JYSettingPwdView *shopNameTextField;
//收货人姓名
@property(nonatomic)JYSettingPwdView *consigneeNameTextField;
//收货人手机号
@property(nonatomic)JYSettingPwdView *consigneePhoneTextField;
//商家地址
@property(nonatomic)JYSettingPwdView *shopAddressTextField;
//详细地址
@property(nonatomic)JYSettingPwdView *addressDetaTextField;
//配送时间
@property(nonatomic)JYSettingPwdView *timeTextField;


@property (nonatomic, strong) HDPickerView *pickerView;

@property (nonatomic, strong) UILabel *addressLabel;

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) JYTimePickerView *timePickerView;


@property(nonatomic)UIView *bgView;
@end

@implementation JYShopsInfoController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商家信息";
    [self setupSubView];
    
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
 
}


#pragma mark - loadData
- (void)loadDataFromNetwork{
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"merchantName"] =  self.shopNameTextField.inputTextFiled.text; //商户名称
    params[@"receivingName"] = self.consigneeNameTextField.inputTextFiled.text; //收货人
    params[@"contactnumber"] = self.consigneePhoneTextField.inputTextFiled.text; //联系方式
    params[@"cityid"] = @"";
    params[@"address"] = self.addressDetaTextField.inputTextFiled.text; //详细地址
    params[@"distributionTime"] = _timeLabel.text;//配送时间
    params[@"userId"] = @"1";
    
    [HTTPRequest POST:kAddUserReceivingAddressUrl parameter:params success:^(id resposeObject) {
       
        if (SUCCESS) {
            
        }else{
            
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {

        [MBProgressHUD LY_ShowError:(kNoNetworkTips) time:2.0];
    }];



}
#pragma mark - setupSubView
- (void)setupSubView{

    CGFloat margin = 0;
    CGFloat height = kGlobalButtonHeight;
    
    CGFloat bgViewHight = height *6;
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, bgViewHight)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    _bgView = bgView;
    

    //商家名称
    self.shopNameTextField = [self registerFrame:CGRectMake(margin, 0, Screen_Width, height) placeholder:@"请输入商家名称" leftImgName:@"login_shop"];
    
    //收货人姓名
    self.consigneeNameTextField = [self registerFrame:CGRectMake(margin, CGRectGetMaxY(self.shopNameTextField.frame),Screen_Width, height) placeholder:@"请输入收货人姓名" leftImgName: @"login_account_number"];
    
    
    //手机号
    self.consigneePhoneTextField  = [self registerFrame:CGRectMake(margin, CGRectGetMaxY(self.consigneeNameTextField.frame), Screen_Width, height) placeholder:@"请输入收货人手机号" leftImgName:@"login_mobile_number"];
    
    //商家地址
    self.shopAddressTextField =  [self registerFrame:CGRectMake(margin, CGRectGetMaxY(self.consigneePhoneTextField.frame), Screen_Width, height) placeholder:@"" leftImgName:@"login_localize"];
    
    _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, self.shopAddressTextField.y, self.shopAddressTextField.width-40, self.shopAddressTextField.height)];
    _addressLabel.text = @"商家地址";
    _addressLabel.textColor = [UIColor colorWithRed:0.78f green:0.78f blue:0.80f alpha:1.00f];
    _addressLabel.font = kFont(16);
    _addressLabel.textAlignment = NSTextAlignmentLeft;
    _addressLabel.userInteractionEnabled = YES;
    _addressLabel.adjustsFontSizeToFitWidth = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAddress)];
    tap.numberOfTapsRequired = 1;
    [_addressLabel addGestureRecognizer:tap];
    [self.bgView addSubview:_addressLabel];
    
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_Width - 20, (self.shopAddressTextField.height-14)/2, 7, 14)];
    imgV.image = [UIImage imageNamed:@"mine_arrow_right"];
    [self.shopAddressTextField addSubview:imgV];
    
    //详细地址
    self.addressDetaTextField =  [self registerFrame:CGRectMake(margin, CGRectGetMaxY(self.shopAddressTextField.frame), Screen_Width , height) placeholder:@"输入详细地址" leftImgName:@""];
    
     //配送时间
     self.timeTextField =  [self registerFrame:CGRectMake(margin, CGRectGetMaxY(self.addressDetaTextField.frame), Screen_Width , height) placeholder:@"配送时间" leftImgName:@"login_time"];
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_Width - 20,(self.timeTextField.height-14)/2, 7, 14)];
    imageView1.image = [UIImage imageNamed:@"mine_arrow_right"];
    [self.timeTextField addSubview:imageView1];
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, self.timeTextField.y, self.timeTextField.width-30, self.timeTextField.height)];
    _timeLabel.textColor = k3Color;
    _timeLabel.font = kFont(16);
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    _timeLabel.userInteractionEnabled = YES;
    _timeLabel.adjustsFontSizeToFitWidth = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTime)];
    tap1.numberOfTapsRequired = 1;
    [_timeLabel addGestureRecognizer:tap1];
    [self.bgView addSubview:_timeLabel];

   //确认信息
    UIButton *confirmBtn = [UIButton jy_buttontitle:@"确认信息" titleColor:[UIColor whiteColor] frame:CGRectMake(10, CGRectGetMaxY(self.bgView.frame)+30, Screen_Width-20, kBtnHeight) target:self action:@selector(confirmOnClick) fontSize:17 bgImageName:@"login_button_background"];
    [self.view addSubview:confirmBtn];


}
#pragma mark - SEL Methods
- (void)confirmOnClick{
    
    [self loadDataFromNetwork];
    
    NSLog(@"%s",__func__);
    
}

- (void)selectTime{


    [self.timePickerView show];

}
- (void)selectAddress {
 
    [self.pickerView show];
}


#pragma mark - Setter && Getter Methods
- (HDPickerView *)pickerView {
    if (!_pickerView) {
        JYWeakSelf;
        _pickerView = [HDPickerView selectArea:^(NSString *province, NSString *city, NSString *area, NSString *streets) {
            weakSelf.addressLabel.textColor = [UIColor blackColor];
            weakSelf.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@", province, city, area, streets];
        }];
    }
    return _pickerView;
}


- (JYTimePickerView *)timePickerView {
    if (!_timePickerView) {
        JYWeakSelf;
        _timePickerView = [JYTimePickerView selectTime:^(NSString *time) {
            weakSelf.timeTextField.inputTextFiled.placeholder = @"";
            weakSelf.timeLabel.text = time;
        }];
    }
    return _timePickerView;
}


- (JYSettingPwdView *)registerFrame:(CGRect)frame placeholder:(NSString *)placeholder leftImgName:(NSString *)leftImgName{
    
    JYSettingPwdView *regiseter = [[JYSettingPwdView alloc] initWithFrame:frame];
    regiseter.inputTextFiled.placeholder = placeholder;
    regiseter.inputTextFiled.font = kFont(16);
    regiseter.imgView.image = [UIImage imageNamed:leftImgName];
    [self.bgView addSubview:regiseter];
    
    return regiseter;
    
    
}

@end
